import pygame, sys, random, time
from pygame.locals import *   
pygame.init()
DISPLAY = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Snake  Cmd')
FPSCLOCK = pygame.time.Clock()
BASICFONT = pygame.font.SysFont("SIMYOU.TTF", 80)
BLACK = pygame.Color(0, 0, 0)WHITE = pygame.Color(255, 255, 255)RED = pygame.Color(255, 0, 0)GREY = pygame.Color(150, 150, 150)
BLACK = pygame.Color(0, 0, 0)
WHITE = pygame.Color(255, 255, 255)
RED = pygame.Color(255, 0, 0)
GREY = pygame.Color(150, 150, 150)
# 贪吃蛇的的初始位置
snake_Head = [100,100]
# 初始化贪吃蛇的长度 (注：这里以20*20为一个标准小格子)
snake_Body = [[80,100],[60,100],[40,100]]
# 指定蛇初始前进的方向，向右
direction = "right"
# 食物位置初始化
food_Position = [300,300]
# 0已被吃掉；1未被吃掉。
food_flag = 1
for event in pygame.event.get():
    if event.type == QUIT:
        # 接收到退出事件后，退出程序
        pygame.quit()
        sys.exit()
   elif event.type == KEYDOWN:
        if (event.key == K_UP or event.key == K_w) and direction != DOWN:
            direction = UP
        if (event.key == K_DOWN or event.key == K_s) and direction != UP:
            direction = DOWN
        if (event.key == K_LEFT or event.key == K_a) and direction != RIGHT:
            direction = LEFT
        if (event.key == K_RIGHT or event.key == K_d) and direction != LEFT:
            direction = RIGHT
if direction == LEFT:
    snake_Head[0] -= 20
if direction == RIGHT:
    snake_Head[0] += 20
if direction == UP:
    snake_Head[1] -= 20
if direction == DOWN:
    snake_Head[1] += 20
snake_Body.insert(0, list(snake_Head))
# 蛇君今天你吃掉食物了吗
if snake_Head[0]==food_Position[0] and snake_Head[1]==food_Position[1]:
    food_flag = 0
else:
    snake_Body.pop()
# 食物RIP
if food_flag == 0:
    # 随机生成x, y
    x = random.randrange(1,32)
    y = random.randrange(1,24)
    food_Position = [int(x*20),int(y*20)]
    food_flag = 1
# 这只蛇体面的出生了
def drawSnake(snake_Body):
    for i in snake_Body:
        pygame.draw.rect(DISPLAY, WHITE, Rect(i[0], i[1], 20, 20))
# 厨师师傅开始做饭了
def drawFood(food_Position):
    pygame.draw.rect(DISPLAY, RED, Rect(food_Position[0], food_Position[1], 20, 20))# 打印出当前得分
def drawScore(score):
    # 设置分数的显示颜色
    score_Surf = BASICFONT.render('%s' %(score), True, GREY)
    # 设置分数的位置
    score_Rect = score_Surf.get_rect()
    score_Rect.midtop = (320, 240)
    # 绑定以上设置到句柄
    DISPLAY.blit(score_Surf, score_Rect)
drawSnake(snake_Body)
# 画出食物的位置
drawFood(food_Position)
# 打印出玩家的分数
drawScore(len(snake_Body) - 3)
# 刷新Pygame的显示层，贪吃蛇与食物的每一次移动，都会进行刷新显示层的操作来显示。
pygame.display.flip()
# 控制游戏速度
FPSCLOCK.tick(7)
# 结束了吗
def GameOver():
    # 设置GameOver的显示颜色
    GameOver_Surf = BASICFONT.render('Game Over!', True, GREY)
    # 设置GameOver的位置
    GameOver_Rect = GameOver_Surf.get_rect()
    GameOver_Rect.midtop = (320, 10)
    # 绑定以上设置到句柄
    DISPLAY.blit(GameOver_Surf, GameOver_Rect)

    pygame.display.flip()
    # 等待3秒
    time.sleep(3)
    # 退出游戏
    pygame.quit()
    # 退出程序
    sys.exit()
# 心电检测仪
if snake_Head[0]<0 or snake_Head[0]>620:
    GameOver()
if snake_Head[1]<0 or snake_Head[1]>460:
    GameOver()
# 贪吃蛇触碰到自己
for i in snake_Body[1:]:
    if snake_Head[0]==i[0] and snake_Head[1]==i[1]:
        GameOver()
# 蛇君卒享年5分钟